/* 
* Copyright (C) 2007 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package android.com.app;


import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

import android.com.app.DBAdapter.Table;

/********** TagActivity Class ************/
/** Main activity which
 		* Displays image in a canvas
 		*  	
 */


public class TagActivity extends GraphicsActivity{

	private static String tagN=new String();
	private DBAdapter dba;
	private static float[] mPts;
	private static float[] tmpPts;
	private static Bundle extras;
	static String u=new String();
	private float tempPts1=0, tempPts2=0;
	private SampleView sv;
	private static boolean dispFlag=false;

@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	sv=new SampleView(this, this);
	dba=new DBAdapter(this);
    extras=getIntent().getExtras();
	setContentView(sv);
}


private static class SampleView extends View{

	private Paint mPaint = new Paint();
	private TagActivity activ;
	private static int dispcntr;
	private Bitmap bmp;
	private final int startposx=55, startposy=25;
	private Cursor cur;

	public SampleView(Context context, TagActivity act) {
		super(context);
		mPts=new float[16];
		tmpPts=new float[4];
		activ=act;
	}


 @Override 
 
 protected void onDraw(Canvas canvas) {
	
	Paint paint = mPaint;
	Matrix mat=new Matrix();
	float[] temarr=new float[4];
	u = extras.getString("Title");
	
	if(bmp==null) {
		BitmapFactory.Options bo = new BitmapFactory.Options();
		bo.inSampleSize=8;  
		bmp = BitmapFactory.decodeFile(u, bo);
	
		mat.postScale(1,1);
		bmp=Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), mat, true);
		paint.setColor(Color.WHITE);
		paint.setStrokeWidth(100);
	}
		canvas.drawBitmap(bmp, startposx, startposy, paint);
		paint.setColor(Color.RED);
		paint.setStrokeWidth(5);
	
		activ.dba.open();
		cur=activ.dba.fetch(Table.TAGS,u);
		activ.dba.close();
		cur.moveToFirst();
		
		while(cur!=null && cur.getCount()>0){
			temarr[0]=cur.getFloat(cur.getColumnIndex("posx1"));
			temarr[1]=cur.getFloat(cur.getColumnIndex("posy1"));
			temarr[2]=cur.getFloat(cur.getColumnIndex("posx2"));
			temarr[3]=cur.getFloat(cur.getColumnIndex("posy2"));
			fillArr(temarr);
			canvas.drawLines(mPts, paint);
			canvas.drawText(cur.getString(cur.getColumnIndex("tag")), temarr[0]+5,temarr[1]+13, 
						paint);
			if(! cur.moveToNext())
					break;
				}
		 
	    if(dispFlag) {
	    	temarr[0]=tmpPts[0];
	    	temarr[1]=tmpPts[1];
	    	temarr[2]=activ.tempPts1;
	    	temarr[3]=activ.tempPts2;
	    	fillArr(temarr);
	    	canvas.drawLines(mPts, paint);
	    }
	    dispFlag=false;
	cur.close();
}


protected void fillArr(float[] tmpPts) {
	mPts[0]=tmpPts[0];
	mPts[1]=tmpPts[1];
	mPts[2]=tmpPts[0];
	mPts[3]=tmpPts[3];
	mPts[4]=tmpPts[0];
	mPts[5]=tmpPts[1];
	mPts[6]=tmpPts[2];
	mPts[7]=tmpPts[1];
	mPts[8]=tmpPts[2];
	mPts[9]=tmpPts[3];
	mPts[10]=tmpPts[2];
	mPts[11]=tmpPts[1];
	mPts[12]=tmpPts[2];
	mPts[13]=tmpPts[3];
	mPts[14]=tmpPts[0];
	mPts[15]=tmpPts[3];
}

public boolean onTouchEvent(MotionEvent event) {
	// TODO Auto-generated method stub
		 	
	if(event.getAction()==MotionEvent.ACTION_UP) {
		if( tmpPts[0] < startposx || tmpPts[1] < startposy || activ.tempPts1 > startposx+bmp.getWidth() 
				  || activ.tempPts2 > startposy+bmp.getHeight())
			return true;
		tmpPts[2]=activ.tempPts1;
		tmpPts[3]=activ.tempPts2;
		Intent in=new Intent(this.getContext(), getString.class);
		in.putExtra("Title",u);
		activ.startActivityForResult(in,1);
		} 
	else if(event.getAction()==MotionEvent.ACTION_DOWN) {
			if(event.getX() < startposx || event.getY()  < startposy ||
					event.getX() > startposx+bmp.getWidth() || event.getY() > startposy+bmp.getHeight())
				return true;
			tmpPts[0]=event.getX();
			tmpPts[1]=event.getY();
		}
	else if(event.getAction()==MotionEvent.ACTION_MOVE) {
		    if(event.getX() > startposx+bmp.getWidth() || event.getY() > startposy+bmp.getHeight()
		    	|| event.getX()<startposx || event.getY()<startposy || tmpPts[0] < startposx)
		    	return true;
			activ.tempPts1=event.getX();
			activ.tempPts2=event.getY();
			if(dispcntr++%2==0) {
				invalidate();
			dispFlag=true;
		}
	}
	return true; 
}
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);
    Bundle extras;
    String title;
       
    switch(requestCode) {
    case 1:
    	if(intent==null) {
    		break;
    	}
    	extras= intent.getExtras();
        title = extras.getString("TagName");
        if(title!=null) {
        	tagN=title;
        	dba.open();
        	dba.AddTag(tmpPts[0], tmpPts[1], tmpPts[2], tmpPts[3], tagN, u);
        	dba.close();
           	  }
            break;
       }
  }

protected void onDestroy() {
	super.onDestroy();
	//sv.bmp.recycle();
	System.gc();
}


protected void onPause() {
	System.gc();
	super.onPause();
	    }

protected void onResume(){
	super.onResume();
	   }

private static final int GAL_ID = Menu.FIRST;
private static final int CAM_ID = Menu.FIRST+1;


@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
    switch(item.getItemId()) {
    case GAL_ID:
    	startActivity(new Intent( this, thumbviewer.class));
        return true;
    case CAM_ID:
    	startActivity(new Intent( this, aPics.class));
    	return true;
    }
    return super.onMenuItemSelected(featureId, item);
}

}




